<%
  var headContent = {
%>
<!-- //这里包含需要引入的其他css/js文件 -->
<style>
 /*验证：提示信息样式 begin*/
 .am-alert-danger {
    background-color: transparent;
    border-color: transparent;
    color: red;
  }
  .am-alert {
    margin-bottom: 1em;
    padding: .625em;
    background: transparent;
    border: none;
    border-radius: 0;
  }
  /*验证：提示信息样式 end*/
</style>
<%};%>
<%layout("/common/_layout.html",{head:headContent}){%>
<div>
    <% include("/admin/waiting_hint.tpl"){}%></div>
<div class="am-cf am-padding">
    <div class="am-fl am-cf"> <strong class="am-text-primary am-text-lg">系统管理</strong>
        /
        <small>店员酬金配置</small>
    </div>
</div>
<!--选项卡（tabs）begin-->
<div class="am-tabs am-margin" data-am-tabs>
    <ul class="am-tabs-nav am-nav am-nav-tabs">
        <li class="am-active">
            <a href="#tab1">店员酬金配置</a>
        </li>
    </ul>

    <div class="am-tabs-bd">
        <%if(noData){%>
        <p class="am-text-warning am-text-center">无配置权限，请联系管理员!</p>
        <%}%>
        <div class="am-tab-panel am-fade am-in am-active" id="tab1">
            <!--验证表单元素（validate) begin-->
            <!--select-single begin-->
            <%if(!noData){%>
            <div class="am-g am-form-group am-margin-top">
                <div class="am-u-sm-4 am-u-md-2 am-text-right">分公司名称</div>
                <div class="am-u-sm-6 am-u-md-6">
                    <select data-am-selected="{btnWidth: '30%', btnSize: 'sm', btnStyle: 'secondary', maxHeight: 400}" name="orgID" id="orgID">
                        <%for(org in orgs){%>
                        <option value="${org.value}" <%if(org.value == orgID){%>
                            selected
                            <%}%>>${org.key}</option>
                        <%}%></select>
                </div>
                <div class="am-u-sm-2 am-u-md-4 input-msg"></div>
            </div>
            <%}%>
            <!--select-single end-->
            <div class="am-g am-form-group am-margin-top">
                <div class="am-u-sm-4 am-u-md-2 am-text-right">模块名称</div>
                <div class="am-u-sm-6 am-u-md-6">该模块封顶金额（单位:元/月,为空或0表示不封顶）</div>
                <div class="am-u-sm-2 am-u-md-4 input-msg" id="role_name_valid"></div>
            </div>

            <!--input begin-->
            <div class="am-g am-form-group am-margin-top">
                <div class="am-u-sm-4 am-u-md-2 am-text-right">充值缴费</div>
                <div class="am-u-sm-6 am-u-md-6">
                    <input name="charge" class="js-ajax-validate" data-validate-async
                                type="text" id="charge" value="${charge!0}" maxlength="100"
                                placeholder="0" onkeyup="replaceContent(this);" <%if(noData){%>
                    disabled="disabled"
                    <%}%>/></div>
                <div class="am-u-sm-2 am-u-md-4 input-msg" id="role_name_valid"></div>
            </div>
            <div class="am-g am-form-group am-margin-top">
                <div class="am-u-sm-4 am-u-md-2 am-text-right">资费办理</div>
                <div class="am-u-sm-6 am-u-md-6">
                    <input name="tarrif" class="js-ajax-validate" data-validate-async
                                type="text" id="tarrif" value="${tarrif!0}" maxlength="100"
                                placeholder="0" onkeyup="replaceContent(this);" <%if(noData){%>
                    disabled="disabled"
                    <%}%>/></div>
                <div class="am-u-sm-2 am-u-md-4 input-msg" id="role_name_valid"></div>
            </div>
            <div class="am-g am-form-group am-margin-top">
                <div class="am-u-sm-4 am-u-md-2 am-text-right">开卡</div>
                <div class="am-u-sm-6 am-u-md-6">
                    <input name="newcard" class="js-ajax-validate" data-validate-async
                                type="text" id="newcard" value="${newcard!0}" maxlength="100"
                                placeholder="0" onkeyup="replaceContent(this);" <%if(noData){%>
                    disabled="disabled"
                    <%}%>/></div>
                <div class="am-u-sm-2 am-u-md-4 input-msg" id="role_name_valid"></div>
            </div>
            <div class="am-g am-form-group am-margin-top">
                <div class="am-u-sm-4 am-u-md-2 am-text-right">实名补登记</div>
                <div class="am-u-sm-6 am-u-md-6">
                    <input name="realname" class="js-ajax-validate" data-validate-async
                                type="text" id="realname" value="${realname!0}" maxlength="100"
                                placeholder="0" onkeyup="replaceContent(this);" <%if(noData){%>
                    disabled="disabled"
                    <%}%>/></div>
                <div class="am-u-sm-2 am-u-md-4 input-msg" id="role_name_valid"></div>
            </div>
            <div class="am-g am-form-group am-margin-top">
                <div class="am-u-sm-4 am-u-md-2 am-text-right">营销活动</div>
                <div class="am-u-sm-6 am-u-md-6">
                    <input name="market" class="js-ajax-validate" data-validate-async
                                type="text" id="market" value="${market!0}" maxlength="100"
                                placeholder="0" onkeyup="replaceContent(this);" <%if(noData){%>
                    disabled="disabled"
                    <%}%>/></div>
                <div class="am-u-sm-2 am-u-md-4 input-msg" id="role_name_valid"></div>
            </div>
            <div class="am-g am-form-group am-margin-top">
                <div class="am-u-sm-4 am-u-md-2 am-text-right">宽带预约</div>
                <div class="am-u-sm-6 am-u-md-6">
                    <input name="broadband" class="js-ajax-validate" data-validate-async
                                type="text" id="broadband" value="${broadband!0}" maxlength="100"
                                placeholder="0" onkeyup="replaceContent(this);" <%if(noData){%>
                    disabled="disabled"
                    <%}%>/></div>
                <div class="am-u-sm-2 am-u-md-4 input-msg" id="role_name_valid"></div>
            </div>
            <div class="am-g am-form-group am-margin-top">
                <div class="am-u-sm-4 am-u-md-2 am-text-right">选号入网</div>
                <div class="am-u-sm-6 am-u-md-6">
                    <input name="selectedNetwork" class="js-ajax-validate" data-validate-async
                                type="text" id="selectedNetwork" value="${selectedNetwork!0}" maxlength="100"
                                placeholder="0" onkeyup="replaceContent(this);" <%if(noData){%>
                    disabled="disabled"
                    <%}%>/></div>
                <div class="am-u-sm-2 am-u-md-4 input-msg" id="role_name_valid"></div>
            </div>
            <div class="am-g am-form-group am-margin-top">
                <div class="am-u-sm-4 am-u-md-2 am-text-right">总封顶金额</div>
                <div class="am-u-sm-6 am-u-md-6">
                    <input name="total" class="js-ajax-validate" data-validate-async
                                type="text" id="total" value="${total!0}" maxlength="100"
                                placeholder="0" onkeyup="replaceContent(this);" <%if(noData){%>
                    disabled="disabled"
                    <%}%>/></div>
                <div class="am-u-sm-2 am-u-md-4 input-msg" id="role_name_valid"></div>
            </div>
            <!--验证表单元素（validate end-->
        </div>
    </div>
</div>
<!--选项卡（tabs）end-->
<%if(so.hasPermission("CLERKREWARD_SAVE") && noData==false) {%>
<div class="am-margin">
    <button type="button" id="submit-btn" class="am-btn am-btn-primary am-btn-xs">提交保存</button>
</div>
<%}%>
<script type="text/javascript" src="${base}/template/admin/js/role.js"></script>
<script type="text/javascript">
        var urlBase = "${base}";
        $(function(){
            $('#orgID').on('change', function(){
                loadOrgClerkreward($(this).val());
            });
            $('#submit-btn').on('click',function(){
                var orgID = $('#orgID').val();
                var charge = $('#charge').val();
                var tarrif = $('#tarrif').val();
                var newcard = $('#newcard').val();
                var realname = $('#realname').val();
                var market = $('#market').val();
                var broadband = $('#broadband').val();
                var selectedNetwork = $('#selectedNetwork').val();
                var total = $('#total').val();
                var btnSubmit = $('#submit-btn');

                showWaitingHint("保存配置项，请稍候...");
                $.ajax({
                    url : urlBase + "/clerkreward/save",
                    type : 'POST',
                    dataType : 'json',
                    data : {
                        orgID : orgID, charge : charge, tarrif : tarrif, newcard : newcard, realname : realname,
                        market : market, broadband : broadband, selectedNetwork : selectedNetwork, total: total
                    },
                    cache : false,
                    beforeSend : function(data) {
                        $('#orgID').selected('disable');
                        $("#submit-btn").prop("disabled", true);
                    }
                }).done(function (result) {
                    if (result) {
                        if(result.status==200){
                            showDialog("操作成功", result.message, "success", 2000);
                        }else if(result.status == 401){
                            showDialog("操作失败", result.message, "error", 2000);
                            setTimeout(directToLoging(result.url), 4000);
                        }else{
                            showDialog("操作失败", result.message, "error", 2000);
                        }
                        dismissWaitingHint();
                        $("#submit-btn").prop("disabled", false);
                        $('#orgID').selected('enable');
                    }
                }).fail(function () {
                    showDialog("操作失败", "", "error", 2000);
                    dismissWaitingHint();
                     $("#submit-btn").prop("disabled", false);
                     $('#orgID').selected('enable');
                });
            });
        });

        function showDialog(msgTitle, message, msgType, msgTimer){
            sendMessage({
                            title : msgTitle,
                            text : message,
                            type : msgType,
                            timer : msgTimer,
                            confirmButtonText : false
                });
        }

        function directToLoging(url){
            top.location.href=urlBase + url;
        }

        function isInt(data) {
              var result = false;
              if (typeof data != 'undefined') {
                  // 正则表达式 匹配 判断是负数 0 正数
                  //var regexp = /^\d*$/;
                  var regexp =  /^[+-]?\d*$/;
                  if (regexp.test(data)) {
                      result = true;
                  } else {
                      result = false;
                  }
              }
              return result;
        }

        function replaceContent(obj){
            obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
            obj.value = obj.value.replace(/^\./g,""); //验证第一个字符是数字而不是
            obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个. 清除多余的
            obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
            obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
        }

        function loadOrgClerkreward(id){
            var orgID = id;
            showWaitingHint("正在加载, 请稍候...");
            $.ajax({
                url : urlBase + "/clerkreward/getDetail",
                type : 'POST',
                dataType : 'json',
                data : {
                    orgID : orgID
                },
                cache : false,
                beforeSend:function(data){
                    $('#orgID').selected('disable');
                    $("#submit-btn").prop("disabled", true);
                }
            }).done(function (result) {
                if (result) {
                    if(result.status==200){
                        $('#charge').val(result.charge);
                        $('#tarrif').val(result.tarrif);
                        $('#newcard').val(result.newcard);
                        $('#realname').val(result.realname);
                        $('#market').val(result.market);
                        $('#broadband').val(result.broadband);
                        $('#selectedNetwork').val(result.selectedNetwork);
                        $('#total').val(result.total);
                    }else if(result.status == 401){
                        showDialog("操作失败", result.message, "error", 2000);
                        setTimeout(directToLoging(result.url), 4000);
                    }else{
                        showDialog("操作失败", result.message, "error", 2000);
                    }
                    dismissWaitingHint();
                    $("#submit-btn").prop("disabled", false);
                    $('#orgID').selected('enable');
                }
            }).fail(function () {
                showDialog("操作失败", "", "error", 2000);
                dismissWaitingHint();
                $("#submit-btn").prop("disabled", false);
                $('#orgID').selected('enable');
            });
        }
      </script>
<%}%>